package io.renren.utils;

import io.renren.entity.ColumnEntity;
import io.renren.entity.TableEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * SQL生成工具
 *
 * @author: WangKun
 * @date: 2020/12/2
 */
public class SqlUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(SqlUtils.class);

    public static Map<String, Object> getAutoCreateSql(TableEntity table) {
        List<ColumnEntity> columsList = table.getColumns();
        String columnFields = columsList.stream().map(ColumnEntity::getColumnName).collect(Collectors.joining(","));
        Map sqlMap = new HashMap();
        String insertSql = getInsertSql(table, columnFields);
        String deleteSql = getDeleteSql(table);
        String updateSql = getUpdateSql(table);
        String selectByIdSql = getSelectByIdSql(table);
        String exampleWhereClause = getExampleWhereClause(table);

        sqlMap.put("columnFields", columnFields);
        sqlMap.put("insert", insertSql);
        sqlMap.put("update", updateSql);
        sqlMap.put("delete", deleteSql);
        sqlMap.put("exampleWhereClause", exampleWhereClause);
        sqlMap.put("selectById", selectByIdSql);

        return sqlMap;
    }

    public static String getInsertSql(TableEntity table, String columnFields) {
        List<ColumnEntity> columsList = table.getColumns();
        String insert = "insert into " + table.getTableName() + "(" + columnFields + ")\n\t\t values(";
        int size = 1;
        StringBuffer sb = new StringBuffer();
        for (ColumnEntity columnEntity : columsList) {
            if (size++ == 1) {
                sb.append("#{" + columnEntity.getAttrname() + "}");
                continue;
            }
            sb.append(",#{" + columnEntity.getAttrname() + "}");
        }
        return insert + sb.toString() + ")";
    }

    public static String getDeleteSql(TableEntity table) {
        String columnName = table.getColumns().get(0).getColumnName();
        return "delete from " + table.getTableName() + " where " + table.getPk().getColumnName() + "=#{" + table.getPk().getAttrname() + "}";
    }

    public static String getUpdateSql(TableEntity table) {
        List<ColumnEntity> columns = table.getColumns();
        StringBuffer sb = new StringBuffer();
        int i = 1;
        for (ColumnEntity column : columns) {
            sb.append(column.getColumnName() + "=#{" + column.getAttrname() + "}");
            if (i++ < columns.size()) {
                sb.append(",");
            }
        }

        return "update " + table.getTableName() + " set " + sb.toString() + " where " + table.getPk().getColumnName() + "=#{" + table.getPk().getAttrname() + "}";
    }

    public static String getSelectByIdSql(TableEntity table) {
        String columnName = table.getColumns().get(0).getColumnName();
        return "select <include refid=\"Base_Column_List\" /> from " + table.getTableName() + " where " + table.getPk().getColumnName() + "=#{" + table.getPk().getAttrname() + "}";
    }

    public static String getExampleWhereClause(TableEntity table) {
        List<ColumnEntity> columns = table.getColumns();
        StringBuffer sb = new StringBuffer();
        for (ColumnEntity column : columns) {
            sb.append("\t<if test=\"").append(column.getAttrname()).append(" != null ");

            if ("java.lang.String" == column.getDataType()) {
                sb.append(" and ").append(column.getAttrname()).append(" != ''");
            }
            sb.append(" \">\n\t\t");
            sb.append(" and " + column.getColumnName() + "=#{" + column.getAttrname() + "}\n");
            sb.append("\t</if>\n");
        }
        return sb.toString();
    }
}
